//
//  ZEmoticonCollectionCell.swift
//  swift-weibo
//
//  Created by zero on 17/3/30.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

class ZEmoticonCollectionCell: UICollectionViewCell {
    
    //  按钮的数组
    var buttonArray:[ZEmoticonCellButton] = [ZEmoticonCellButton]()
    
    //  模型数组
    var dataList:[ZEmoticonModel]?{
        
        didSet{
            //  隐藏所有按钮
            
            for btn in buttonArray{
                
                btn.isHidden = true
            }
            for (i,model) in (dataList?.enumerated())!{
                
                let btn = buttonArray[i];
                
                btn.isHidden = false
                
                btn.emotionModel = model
                
            
            }
           
        }
    }
    
    //  接收位置信息
    var indexPath:IndexPath?{
        
        didSet{
            
            labIndexPath.text = "第\(indexPath!.section)组,第\(indexPath!.item)行"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //  布局
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let buttonW:CGFloat = (ScreenWidth - 10) / CGFloat(Col)
        let buttonH:CGFloat = (216 - 35) / CGFloat(Row)
        
        //  遍历数组
        for (i,btn) in buttonArray.enumerated(){
            
            let row = CGFloat(i / Col)
            let col = CGFloat(i % Col)
            
            btn.frame = CGRect(x: col * buttonW, y: row * buttonH, width: buttonW, height: buttonH)
            
        }
        
        btnDeleta.frame = CGRect(x: ScreenWidth - buttonW - 5, y: 2 * buttonH, width: buttonW, height: buttonH)
    }
    //  布局
    private func setupUI(){
        
        //  添加按钮
        addButton()
        
        //  添加删除按钮
        contentView.addSubview(btnDeleta)
        //  添加标示
        contentView.addSubview(labIndexPath)
        
        //  约束
        labIndexPath.snp.makeConstraints { (make) in
            
            make.center.equalTo(contentView)
        }
        
    }
    
    //  懒加载
    fileprivate lazy var labIndexPath = UILabel()
    
    fileprivate lazy var btnDeleta:UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        
        btn.addTarget(self, action: #selector(deleteBtnClick), for: .touchUpInside)
        return btn
        
    }()
    
}

//MARK: 创建 20 个按钮

extension ZEmoticonCollectionCell{
    
    fileprivate func addButton(){
        
        for _ in 0..<NumberOfPage{
            let btn = ZEmoticonCellButton()
            
            btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            
            buttonArray.append(btn)
            
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 30)
            
            contentView.addSubview(btn)
        }
    }
    
    
    //  按钮的点击事件
    @objc fileprivate func btnClick(btn:ZEmoticonCellButton){
        
        //  发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: EMOJINOTIFI), object: btn.emotionModel)
        
    }
    
    //  删除按钮的点击事件
    @objc fileprivate func deleteBtnClick(deleteBtn:UIButton){
        
        //  发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: DELETEBTNCLICK), object:nil)
        
    }
    
}


